import cilp
from time import strftime

def log(tag,msg):
    print(f"{strftime('%H:%M:%S')} [{tag}]:{msg}")


def get_clipboard_text():
    try:
        return cilp.get_clipboard_text()
    except:
        raise
    

def set_clipboard_text(text):
    cilp.copy_to_clipboard(text)
    

def get_peers():
    peers_list = "peers.list"
    peers_file = open(peers_list,"r",encoding="utf-8")
    peers = []
    try:
        line = peers_file.readline()
    except UnicodeDecodeError:
        log("ERROR",f"UTF-8 decode error. Please make sure you are using UTF-8, or change to the encoding you like in source code")
        line = None
    while line is not None and line != "":
        if line.startswith("#") or line.startswith("\n"):
            line = peers_file.readline()
            continue
        try:
            line = line.strip()
            ip_addr,port = line.split(":",2)
            port = int(port)
            peers.append((ip_addr,port))
        except ValueError:
            log("ERROR",f"Syntax errors in file {peers_list}:{repr(line)}")
        finally:
            line = peers_file.readline()
        
    if peers == []:
        log("Warning","There are't peers to synchronize.")
    return peers

